package br.ucb.antares.HIB;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.exception.ConstraintViolationException;

import br.ucb.antares.bean.FornecedorBean;
import br.ucb.antares.entidade.Fornecedor;
import br.ucb.antares.interfaces.HIB;
import br.ucb.antares.log4j.FornecedorLog;

public class FornecedorHIB implements HIB<Fornecedor>{
	public void salvar(Fornecedor fornecedor) {
		Session session = HibernateUtil.getSession();
		Transaction transaction = session.beginTransaction();
		session.saveOrUpdate(fornecedor);
		transaction.commit();
		session.close();
	}
	public void excluir(Fornecedor fornecedor) throws ConstraintViolationException {
		Session session = HibernateUtil.getSession();
		Transaction transaction = session.beginTransaction();
		session.delete(fornecedor);
		transaction.commit();
		session.close();
	}
	@SuppressWarnings("unchecked")
	public List<Fornecedor> listar() {
		Session session = HibernateUtil.getSession();
		try {
			return session.createCriteria(Fornecedor.class).list();
		} finally {
			session.close();
		}
	}
	public Fornecedor consultar(long id) {
		Session session = HibernateUtil.getSession();
		try {
			Fornecedor fornecedor = (Fornecedor) session.get(Fornecedor.class, id);
			return fornecedor;
		} finally {
			session.close();
		}
	}
}